﻿Imports Aricie.DNN.UI.Controls
Imports System.Data

Public Class ItemTemplate
    Inherits AricieUserControlBase
    Implements ITemplate


#Region "Private Attributes"
    Private _fieldName As String
    Private _isDate As Boolean
#End Region


#Region "Constructeur"
    Public Sub New(ByVal fieldName As String, ByVal isDate As Boolean)
        _fieldName = fieldName
        _isDate = isDate
    End Sub
#End Region

#Region "Implement ITemplate"
    Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
        Dim myLblField As New Label
        AddHandler myLblField.DataBinding, AddressOf tbl_DataBindingLabel
        myLblField.ID = _fieldName
        container.Controls.Add(myLblField)
    End Sub
#End Region


#Region "Methods"
    Sub tbl_DataBindingLabel(ByVal sender As Object, ByVal e As EventArgs)

        Dim lbldata As Label = CType(sender, Label)
        Dim container As GridViewRow = CType(lbldata.NamingContainer, GridViewRow)


        Dim dataValue As Object = Nothing
        If _isDate Then
            Dim dateFormat As String = CurrentCulture.DateTimeFormat.ShortDatePattern
            dataValue = DataBinder.Eval(container.DataItem, _fieldName, "{0:" + dateFormat + "}")
            'dataValue = DataBinder.Eval(container.DataItem, _fieldName)
        Else
            Dim objDataRowView As DataRowView = DirectCast(container.DataItem, DataRowView)
            If Not IsDBNull(objDataRowView.Item(_fieldName)) Then
                dataValue = DataBinder.Eval(container.DataItem, _fieldName)
            End If
        End If

        If Not IsNothing(dataValue) Then
            lbldata.Text = dataValue.ToString
        End If

    End Sub
#End Region


End Class
